package cn.schoolwow.util.module.beanCopy.flow;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import com.alibaba.fastjson.JSON;

import java.util.Collection;

public class DeepCloneFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        Object source = flowContext.checkData("source");

        String jsonString = JSON.toJSONString(source);
        if(jsonString.startsWith("{")){
            Object cloneObject = JSON.parseObject(jsonString).toJavaObject(source.getClass());
            flowContext.putReturnData("cloneObject", cloneObject);
        }else if(jsonString.startsWith("[")){
            Collection collection = (Collection) source;
            String className = collection.iterator().next().getClass().getName();
            Object cloneArray = JSON.parseArray(jsonString).toJavaList(ClassLoader.getSystemClassLoader().loadClass(className));
            flowContext.putReturnData("cloneObject", cloneArray);
        }
    }

    @Override
    public String name() {
        return "深克隆对象";
    }
}
